package com.vict.entity;

import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.IEnum;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.vict.enums.app.Platform;
import com.vict.framework.fastjsonserializer.*;
import com.vict.framework.myannotation.MyDescription;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;

import java.math.BigDecimal;
import java.sql.Timestamp;

@Data
@ApiModel
@TableName("app_version")
public class AppVersion {

    @TableId
    @ApiModelProperty("id")
    @JSONField(serializeUsing = LongSerializer.class, deserializeUsing = LongDeserializer.class)
    private Long id;

    @TableField("platform")
    @ApiModelProperty("平台 Android IOS")
    @JSONField(serializeUsing = EnumSerializer.class, deserializeUsing = EnumDeserializer.class)
    private Platform platform;

    @TableField("version_num")
    @ApiModelProperty("数字版本号 000 000 000")
    @JSONField(serializeUsing = IntegerSerializer.class, deserializeUsing = IntegerDeserializer.class)
    private Integer versionNum;

    @TableField("version_str")
    @ApiModelProperty("版本号")
    private String versionStr;

    public static Integer convertVersionNum(String versionStr){
        String versionNumStr = "";
        String[] split = versionStr.split("\\.");
        for(String item : split){
            BigDecimal divide = new BigDecimal(item).divide(new BigDecimal(1000), 3, BigDecimal.ROUND_FLOOR);
            String substring = divide.toPlainString().substring(2);
            versionNumStr = substring + versionNumStr;
        }

        return Integer.parseInt(versionNumStr);
    }

    @TableField("release_time")
    @ApiModelProperty("发布时间")
    @JSONField(serializeUsing = TimestampSerializer.class, deserializeUsing = TimestampDeserializer.class)
    private Timestamp releaseTime;

    @TableField("info")
    @ApiModelProperty("更新内容,功能介绍")
    private String info;

    @TableField("file_url")
    @ApiModelProperty("更新包")
    private String fileUrl;

    @TableField("status")
    @ApiModelProperty("状态, test release")
    @JSONField(serializeUsing = EnumSerializer.class, deserializeUsing = EnumDeserializer.class)
    private Status status;

    /** 状态 */
    @MyDescription("状态")
    @Getter
    @AllArgsConstructor
    public enum Status implements IEnum<String> {
        test("test", "测试中"),
        release("release", "已发布"),
        ;
        private String value;
        private String name;

        @Override
        public String toString() {
            return "Status{" +
                    "value='" + value + '\'' +
                    ", name='" + name + '\'' +
                    '}';
        }
    }


    @TableField("wgt_url")
    @ApiModelProperty("热更新包")
    private String wgtUrl;

    @TableField("update_method")
    @ApiModelProperty("更新方式 APK WGT")
    @JSONField(serializeUsing = EnumSerializer.class, deserializeUsing = EnumDeserializer.class)
    private UpdateMethod updateMethod;

    @MyDescription("更新方式")
    @Getter
    @AllArgsConstructor
    public enum UpdateMethod implements IEnum<String> {
        APK("APK", "APK包"),
        WGT("WGT", "WGT热更新"),
        IPA("IPA", "IPA包"),
        ;
        private String value;
        private String name;

        @Override
        public String toString() {
            return "UpdateMethod{" +
                    "value='" + value + '\'' +
                    ", name='" + name + '\'' +
                    '}';
        }
    }

    @TableField("is_force")
    @ApiModelProperty("是否强制 yes no")
    @JSONField(serializeUsing = EnumSerializer.class, deserializeUsing = EnumDeserializer.class)
    private IsForce isForce;

    @MyDescription("更新方式")
    @Getter
    @AllArgsConstructor
    public enum IsForce implements IEnum<String> {
        yes("yes", "强制更新"),
        no("no", "否"),
        ;
        private String value;
        private String name;

        @Override
        public String toString() {
            return "UpdateMethod{" +
                    "value='" + value + '\'' +
                    ", name='" + name + '\'' +
                    '}';
        }
    }
}
